﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Drawing.Printing;
using System.Reflection;
using HF.Software.Entities;
using HF.Software.ClassLib;

namespace HF.Software
{
    /// <summary>
    /// hef创建于2015.02.04
    /// 会员配置文件读写
    /// </summary>
    public partial class frmMem : Form
    {
        #region event

        /// <summary>
        /// 保存Ini文件配置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (null == this.oConfig.MEM) return;

            IniModelDataCollection oDataCollection = new IniModelDataCollection();
            IniWrite oIniWrite = new IniWrite();

            MEM oMem = this.oConfig.MEM;

            try
            {
                PropertyInfo[] oFields = oMem.GetType().GetProperties();

                foreach (PropertyInfo oInfo in oFields)
                {
                    IniModel oConfigIni = new IniModel();
                    oConfigIni.IN_Section = "MEM";
                    oConfigIni.IN_Key = oInfo.Name;
                    oConfigIni.IN_Value = Convert.ToString(oInfo.GetValue(oMem, null));

                    switch (oConfigIni.IN_Key.Trim())
                    {
                        case "COMPort"://识读器连接端口
                            oConfigIni.IN_Value = this.cmbReadPort.Text.Trim();
                            break;

                        case "CardType"://读卡器卡类型
                            oConfigIni.IN_Value = this.cmbCardType.SelectedValue.ToString().Trim();
                            break;

                        case "BaudRate"://识读器波特率
                            oConfigIni.IN_Value = this.txtBaudRate.Text.Trim();
                            break;

                        case "ReadTime"://读卡间隔毫秒
                            oConfigIni.IN_Value = this.txtIntervalDate.Text.Trim();
                            break;

                        case "LPTPort"://回单打印端口
                            oConfigIni.IN_Value = this.cmbPrintPort.Text.Trim();
                            break;

                        case "Printer"://回单打印设备
                            oConfigIni.IN_Value = this.cmbPrinter.Text.Trim();
                            break;

                        case "PrintWidth"://回单打印宽度
                            oConfigIni.IN_Value = this.cmbPrintWidth.Text.Trim();
                            break;

                        case "PassWord"://开户初始密码
                            oConfigIni.IN_Value = this.txtIntialPwd.Text.Trim();
                            break;
                    }

                    oDataCollection.Add(oConfigIni);
                }

                foreach (IniModel oIniModel in oDataCollection)
                {
                    oIniWrite.Write(oIniModel.IN_Section, oIniModel.IN_Key, oIniModel.IN_Value);
                }
                MessageBox.Show("会员更新成功！", "提示");
            }
            catch (Exception ex)
            {
                this.oLog.fnLog("201502051653会员更新失败：" + ex.ToString());
                MessageBox.Show("会员更新失败！", "提示");
            }
        }

        /// <summary>
        /// 验证回单打印设备是否可用
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmbPrintPort_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.cmbPrinter.Enabled = true;

            if (string.IsNullOrWhiteSpace(this.cmbPrintPort.Text.ToString())) return;

            this.cmbPrinter.Enabled = false;
        }

        #endregion

        #region fun

        /// <summary>
        /// ComboBox绑定数据
        /// </summary>
        /// <param name="_oCmb">ComboBox</param>
        /// <param name="_lstItem">数据源</param>
        private void fnBindComboBox(ComboBox _oCmb, List<ClsComboBoxItem> _lstItem)
        {
            _oCmb.DataSource = _lstItem;
            _oCmb.DisplayMember = "Text";
            _oCmb.ValueMember = "Value";
            _oCmb.SelectedIndex = 0;
        }

        /// <summary>
        /// 初始化 ComboBox数据源
        /// </summary>
        private void fnDataBind()
        {
            #region 识读器连接端口

            List<ClsComboBoxItem> lstCmbReadPort = new List<ClsComboBoxItem>();

            ClsComboBoxItem oCmbReadPort1 = new ClsComboBoxItem();
            oCmbReadPort1.Value = "COM1";
            oCmbReadPort1.Text = "COM1";
            lstCmbReadPort.Add(oCmbReadPort1);

            ClsComboBoxItem oCmbReadPort2 = new ClsComboBoxItem();
            oCmbReadPort2.Value = "COM2";
            oCmbReadPort2.Text = "COM2";
            lstCmbReadPort.Add(oCmbReadPort2);

            ClsComboBoxItem oCmbReadPort3 = new ClsComboBoxItem();
            oCmbReadPort3.Value = "COM3";
            oCmbReadPort3.Text = "COM3";
            lstCmbReadPort.Add(oCmbReadPort3);

            ClsComboBoxItem oCmbReadPort4 = new ClsComboBoxItem();
            oCmbReadPort4.Value = "COM4";
            oCmbReadPort4.Text = "COM4";
            lstCmbReadPort.Add(oCmbReadPort4);

            this.fnBindComboBox(this.cmbReadPort, lstCmbReadPort);

            this.cmbReadPort.SelectedValue = this.oConfig.MEM.COMPort;

            #endregion

            #region 读卡器卡类型

            List<ClsComboBoxItem> lstCmbCardType = new List<ClsComboBoxItem>();

            ClsComboBoxItem oCmbCardType1 = new ClsComboBoxItem();
            oCmbCardType1.Value = "1";
            oCmbCardType1.Text = "IC卡";
            lstCmbCardType.Add(oCmbCardType1);

            ClsComboBoxItem oCmbCardType2 = new ClsComboBoxItem();
            oCmbCardType2.Value = "2";
            oCmbCardType2.Text = "ID卡";
            lstCmbCardType.Add(oCmbCardType2);

            ClsComboBoxItem oCmbCardType3 = new ClsComboBoxItem();
            oCmbCardType3.Value = "3";
            oCmbCardType3.Text = "磁条卡";
            lstCmbCardType.Add(oCmbCardType3);

            this.fnBindComboBox(this.cmbCardType, lstCmbCardType);

            this.cmbCardType.SelectedValue = this.oConfig.MEM.CardType;

            #endregion

            #region 回单打印端口

            List<ClsComboBoxItem> lstCmbPrintPort = new List<ClsComboBoxItem>();

            ClsComboBoxItem oCmbPrintPort0 = new ClsComboBoxItem();
            oCmbPrintPort0.Value = "";
            oCmbPrintPort0.Text = "";
            lstCmbPrintPort.Add(oCmbPrintPort0);

            ClsComboBoxItem oCmbPrintPort1 = new ClsComboBoxItem();
            oCmbPrintPort1.Value = "LPT1";
            oCmbPrintPort1.Text = "LPT1";
            lstCmbPrintPort.Add(oCmbPrintPort1);

            ClsComboBoxItem oCmbPrintPort2 = new ClsComboBoxItem();
            oCmbPrintPort2.Value = "LPT2";
            oCmbPrintPort2.Text = "LPT2";
            lstCmbPrintPort.Add(oCmbPrintPort2);

            ClsComboBoxItem oCmbPrintPort3 = new ClsComboBoxItem();
            oCmbPrintPort3.Value = "LPT3";
            oCmbPrintPort3.Text = "LPT3";
            lstCmbPrintPort.Add(oCmbPrintPort3);

            ClsComboBoxItem oCmbPrintPort4 = new ClsComboBoxItem();
            oCmbPrintPort4.Value = "COM1";
            oCmbPrintPort4.Text = "COM1";
            lstCmbPrintPort.Add(oCmbPrintPort4);

            ClsComboBoxItem oCmbPrintPort5 = new ClsComboBoxItem();
            oCmbPrintPort5.Value = "COM2";
            oCmbPrintPort5.Text = "COM2";
            lstCmbPrintPort.Add(oCmbPrintPort5);

            ClsComboBoxItem oCmbPrintPort6 = new ClsComboBoxItem();
            oCmbPrintPort6.Value = "COM3";
            oCmbPrintPort6.Text = "COM3";
            lstCmbPrintPort.Add(oCmbPrintPort6);

            ClsComboBoxItem oCmbPrintPort7 = new ClsComboBoxItem();
            oCmbPrintPort7.Value = "COM4";
            oCmbPrintPort7.Text = "COM4";
            lstCmbPrintPort.Add(oCmbPrintPort7);

            ClsComboBoxItem oCmbPrintPort8 = new ClsComboBoxItem();
            oCmbPrintPort8.Value = "USB";
            oCmbPrintPort8.Text = "USB";
            lstCmbPrintPort.Add(oCmbPrintPort8);

            this.fnBindComboBox(this.cmbPrintPort, lstCmbPrintPort);

            this.cmbPrintPort.SelectedValue = this.oConfig.MEM.LPTPort;

            #endregion

            #region 回单打印设备

            List<ClsComboBoxItem> lstPrinter = new List<ClsComboBoxItem>();

            foreach (string sPrint in PrinterSettings.InstalledPrinters)//获取所有打印机名称
            {
                ClsComboBoxItem POSPrinter = new ClsComboBoxItem();
                POSPrinter.Value = sPrint;
                POSPrinter.Text = sPrint;
                lstPrinter.Add(POSPrinter);
            }

            this.fnBindComboBox(this.cmbPrinter, lstPrinter);

            this.cmbPrinter.SelectedValue = this.oConfig.MEM.Printer;

            #endregion

            #region 回单宽度

            List<ClsComboBoxItem> lstWidth = new List<ClsComboBoxItem>();

            ClsComboBoxItem POSWidth0 = new ClsComboBoxItem();
            POSWidth0.Value = "80";
            POSWidth0.Text = "80";
            lstWidth.Add(POSWidth0);

            ClsComboBoxItem POSWidth1 = new ClsComboBoxItem();
            POSWidth1.Value = "58";
            POSWidth1.Text = "58";
            lstWidth.Add(POSWidth1);

            this.fnBindComboBox(this.cmbPrintWidth, lstWidth);

            this.cmbPrintWidth.SelectedValue = this.oConfig.MEM.PrintWidth;

            #endregion
        }

        #endregion

        #region create

        public frmMem()
        {
            InitializeComponent();
        }

        private void frmMem_Load(object sender, EventArgs e)
        {
            if (null == this.oConfig) return;

            this.fnDataBind();

            this.txtBaudRate.Text = this.oConfig.MEM.BaudRate.ToString();
            this.txtIntervalDate.Text = this.oConfig.MEM.ReadTime.ToString();
            this.txtIntialPwd.Text = this.oConfig.MEM.PassWord;
        }

        #endregion

        #region var

        AppConfig oConfig = new AppConfig();
        private ClsLog oLog = new ClsLog(Application.StartupPath + @"\log\set\" + DateTime.Now.ToString("yyyyMMddHH") + ".txt");

        #endregion

    }
}
